/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import { cloneDeep } from '@baize/wdk';
import { Input, InputFile, LocalFileWriter, OutputFile } from '@ohos/hvigor';

import { projectOhosConfigManager } from '../common/global/project-ohos-config-manager.js';
import { BundleType } from '../const/common-const.js';
import { ProjectModelImpl } from '../model/project/project-model-impl.js';
import { AppOpt } from '../options/build/app-ohos-config.js';
import { AppJson } from '../options/configure/app-json-options.js';
import { normalizedFileData } from '../utils/build-profile-utils.js';
import { OhosLogger } from '../utils/log/ohos-logger.js';
import { getBuildModeName } from '../utils/task-util.js';
import { TaskNames } from './common/task-names.js';
import { TargetTaskService } from './service/target-task-service.js';
import { OhosHapTask } from './task/ohos-hap-task.js';

import CommonTask = TaskNames.CommonTask;
import AppOptObj = AppJson.AppOptObj;

export interface BuildProfileData {
  BUNDLE_NAME: string;
  BUNDLE_TYPE: string;
  TARGET_NAME: string;
  PRODUCT_NAME: string;
  BUILD_MODE_NAME: string;
  DEBUG: boolean;
  VERSION_CODE: number;
  VERSION_NAME: string;
}

export class BuildProfileTask extends OhosHapTask {
  private _log = OhosLogger.getLogger(BuildProfileTask.name);
  protected readonly buildTaskService: TargetTaskService;
  protected buildProfileData: BuildProfileData | undefined;

  constructor(taskService: TargetTaskService) {
    super(taskService, CommonTask.CREATE_BUILD_PROFILE);
    this.buildTaskService = taskService;
    this.initDefaultData();
  }

  protected initDefaultData(): void {
    const appOpt: AppOptObj = (this.projectModel as ProjectModelImpl).getAppRes().getAppResOpt();
    const appOptFromConfig: AppOpt = cloneDeep(this.projectOhosConfigAppOpt);
    const currentProduct = this.targetData.getProduct();
    this.buildProfileData = {
      BUNDLE_NAME: appOptFromConfig?.bundleName ?? this.targetData.getProduct().bundleName ?? this.projectModel.getDefaultBundleName(),
      BUNDLE_TYPE: appOptFromConfig?.bundleType ?? currentProduct.bundleType ?? appOpt.app.bundleType ?? BundleType.APP,
      VERSION_CODE: appOptFromConfig?.versionCode ?? currentProduct.versionCode ?? appOpt.app.versionCode,
      VERSION_NAME: appOptFromConfig?.versionName ?? currentProduct.versionName ?? appOpt.app.versionName,
      TARGET_NAME: this.targetName,
      PRODUCT_NAME: this.targetData.getProduct().name,
      BUILD_MODE_NAME: this.buildModeName,
      DEBUG: this.buildTaskService.isDebug(),
    };
  }

  // 当前模块的buildMode
  @Input() private get buildMode(): string {
    return this.targetService.getBuildMode();
  }
  // 当前模块的buildModeName
  @Input() private get buildModeName(): string {
    return getBuildModeName();
  }
  // 模块级build-profile.json5中所有自定义buildProfileFields的变量
  @Input() private get buildProfileFields(): object | undefined {
    return this.targetService.getBuildProfileFields();
  }
  @Input() private get projectOhosConfigAppOpt(): AppOpt | undefined {
    return projectOhosConfigManager.getOverrides()?.appOpt;
  }
  // 增量输入文件-app.json5
  @InputFile() private get appResJsonPath(): string {
    return (this.service.getProjectModel() as ProjectModelImpl).getAppRes().getJsonPath();
  }
  // 增量输入文件-工程级的build-profile.json5
  @InputFile() private get projectProfilePath(): string {
    return this.projectModel.getProfilePath();
  }
  // 输出产物-BuildProfile.ets
  @OutputFile() private get buildProfilePath(): string {
    return this.pathInfo.getGenerateBuildProfilePath(this.targetService.getTargetData().getTargetName());
  }

  initTaskDepends(): void {}

  protected doTaskAction(): void {
    const buildProfileParams = this.buildProfileFields ?
      { ...this.buildProfileData, ...this.buildProfileFields } : { ...this.buildProfileData };
    LocalFileWriter.getInstance().writeStr(
      this.buildProfilePath, normalizedFileData(buildProfileParams, this.targetData, this._log),
    );
  }
}
